package com.design.twords.controller;

import java.util.Random;

/**
 * Create by Alen
 * Time:  13:19
 */
public class NumberController {

    //包含两个端点的区间里的一个随机数
    public static int getRandomNumber(int min, int max){
        if (min != max){
            Random random = new Random();
            return random.nextInt(max) % (max - min + 1) + min;
        }else {
            return min;
        }
    }

    //得到区间内的 n 个随机数
    public static  int[] getRandomNumberList(int min, int max, int numb){
        if (numb > (max - min +1) || min > max){
            return null;
        }
        int[] rest = new int [numb];
        int count = 0;
        while(count < numb){
            boolean flag = true;
            int ran = getRandomNumber(min, max);
            for(int i = 0; i < count; i++){
                if (rest[i] == ran){
                    flag = false;
                    break;
                }
            }
            if (flag){
                rest[count] = ran;
                count++;
            }
        }
        return rest;
    }

}
